package mr.go.yaxc.tests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertNull;

import java.net.URL;
import java.util.Map;

import mr.go.yaxc.ActionMatch;
import mr.go.yaxc.Actions;
import mr.go.yaxc.XmlConfigurator;
import mr.go.yaxc.actions.SetObjectPropertyInfo;
import mr.go.yaxc.tests.test2.Something;
import mr.go.yaxc.tests.test2.SomethingHolder;

import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

public class Test2WithNamespaces {

    @BeforeClass
    public static void setUpBeforeClass()
            throws Exception {
        xmlUrlWithNamespaces = Test2.class.getClassLoader().getResource(
                "test2_namespaces.xml");
        if (xmlUrlWithNamespaces == null) {
            throw new IllegalStateException("No xml file");
        }
        final SetObjectPropertyInfo actionInfo = new SetObjectPropertyInfo(
                ActionMatch.LEVEL,
                "",
                200);
        actionInfo.setLevel(2);
        Actions.registerAction(actionInfo);
    }

    @AfterClass
    public static void tearDownAfterClass()
            throws Exception {
        Actions.resetConfiguration();
    }
    private XmlConfigurator configurator2;
    private SomethingHolder somethingHolder2;

    @Before
    public void setUp()
            throws Exception {
        configurator2 = new XmlConfigurator();
        configurator2.setNamespaceAware(true);
        configurator2.setNamespaceUri("http://code.google.com/p/yaxc");
        somethingHolder2 = new SomethingHolder();
        configurator2.setExternalReference(
                "externalSomethingHolder",
                somethingHolder2);
        configurator2.parse(xmlUrlWithNamespaces);
    }

    @Test
    public void testArrayCreationAndStaticMethodCall()
            throws Exception {
        Something[] somethings = somethingHolder2.getSomethings();
        assertEquals(4,
                     somethings.length);
    }

    @Test
    public void testNamespaceOnly()
            throws Exception {
        Map<String, Integer> names = somethingHolder2.getNames();
        assertNull(names);
    }

    @Test
    public void testCreate()
            throws Exception {
        Something sth = somethingHolder2.getSpecialSomething();
        assertEquals("a",
                     sth.getString());
        assertEquals(2,
                     sth.getInteger());
    }

    @Test
    public void testSetObjectProperty()
            throws Exception {
        Something sth = somethingHolder2.getOrdinarySomething();
        assertTrue(sth.isValue());
    }
    private static URL xmlUrlWithNamespaces;
}
